/**
 * Created by lxl on 2017/5/10.
 */
layui.use(['form'], function () {
    var form = layui.form();
    var user = {};
    $(document).ready(function () {
        if (window.location.hash != null && window.location.hash != "") {
            var has = window.location.hash;
            has = has.replace("#", "");
            has = has.replace(/\'/g, "\"");
            user = JSON.parse(has);
        }
        autoInject(user) ;
        form.render() ;
        form.on('submit(yes)', function (data) {
            var  url ;
            if( user.id == undefined ){
                url = "/system/user/add"
            }else{
                url = "/system/user/update" ;
            }
            user = autoFetch(user) ;
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(user),
                dataType: "json",
                success: function (data){
                    if(data.success){
                        layer.msg("增加用户成功") ;
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    }else{
                        layer.msg("增加用户失败") ;
                    }
                },
                error: function (message) {
                    layer.msg("增加用户失败") ;
                }
            });
            return false;
        });

        $('#cancel').click(function(){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            return false ;
        }) ;
    });
});
